home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 076-100 / scopedisk86 / fonz / fonz.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-19  |  7.9 KB  |  271 lines

  1. /*
  2.  * fonz - setfont for program that opens its own screen
  3.  *
  4.  * April 18, 1989
  5.  * A hack to subtitute my own font for Handshake.
  6.  * With a little more work, could be turn into a general purpose
  7.  * setfont for program openning its own screen. I am too lazy to do it.
  8.  *
  9.  * April 19, 1989
  10.  * Let's rename it "fonz".
  11.  * Make necessary changes so that "fonz" can be used in general.
  12.  * Usage: %s [-s font_size] [-w wait_max] font_name screen [window]
  13.  *                     -s font_size: default to 8
  14.  *                     -w wait_max: number of times to go around the wait loop
  15.  *                     font_name: name of font
  16.  *                     screen: screen name
  17.  *                     window: name
  18.  * With no argument:
  19.  *     "fonz" printed out all the screens each followed by its windows
  20.  *
  21.  * If no window is specified, "fonz" will pick out the first NULL window
  22.  *
  23.  * April 20, 1989
  24.  * Let's call thist Version 1.0
  25.  * Bug found:
  26.  *     Use the [-w] flag, "run" the application. For example,
  27.  * runback c:fonz -w 12 clean Handshake; run df2:c/handshake
  28.  * Now, do something in your CLI while the application is loaded.
  29.  * Do a "status", for example, "fonz" will pretend that it does its job
  30.  * and exist but no font is changed to the application's window.
  31.  *
  32.  * Fix:
  33.  *     I don't know the reason for the bug. Attempt to fix by "delaying"
  34.  * 10 secs before do SetFont(). Seems to work OK now.
  35.  * If you know why please let me know.
  36.  *
  37.  * Hung Le (mott@ucscb.ucsc.edu)
  38.  *  
  39.  * compiled under Manx v3.4a (someday I will upgrade ... )
  40.  * 1> cc fonz.c 
  41.  * 1> ln fonz.o -lc 
  42.  */
  43.  
  44. #include <intuition/intuitionbase.h>
  45. #include <libraries/diskfont.h>
  46. #include <graphics/rastport.h>
  47. #include <graphics/text.h>
  48. #include <functions.h>
  49. #include <stdio.h>
  50.  
  51. #define INTUI_REV 0L
  52. #define usage(n) fprintf(stderr,"Usage: %s [-s font_size] [-w wait_max] font_na
  53. e screen [window]\n", n)
  54. /* 500 ticks */
  55. #define TEN_SECS 500L
  56.  
  57. struct IntuitionBase *IntuitionBase = 0L;
  58. struct Window *Window = 0L;
  59. struct TextFont *Font = 0L;
  60. long DiskfontBase = 0L;
  61. long GfxBase = 0L;
  62.  
  63. int Font_Size = 0;
  64. int Wait_Max = 0;
  65.  
  66. /* not parsing workbench */
  67. _wb_parse() {}
  68.  
  69. main(ac,av)
  70. int ac;
  71. char *av[];
  72. {
  73.        char my_name[20];
  74.  
  75.        strcpy(my_name, av[0]);
  76.  
  77.        /* Open the necessary libraries */
  78.        Open_Libs();
  79.  
  80.        if (ac == 1)
  81.                print_windows();
  82.        else    
  83.        {
  84.                /* two global variables: Font_Size and Wait_Max are set */
  85.                while (av[1][0] == '-')
  86.                {
  87.                        if (strcmp(av[1], "-s") == 0)
  88.                        {
  89.                                Font_Size = atoi(av[2]);
  90.                                ac -= 2; av += 2;
  91.                        }
  92.                        else if (strcmp(av[1], "-w") == 0)
  93.                        {
  94.                                Wait_Max = atoi(av[2]);
  95.                                ac -= 2; av += 2;
  96.                        }
  97.                        else
  98.                        {
  99.                                /* ignore bad flag */
  100.                                ac--; av++;
  101.                        }
  102.                }
  103.  
  104.                /*
  105.                 * Make sure that at least the following two
  106.                 * variables are set to default values
  107.                 */
  108.                if (Font_Size <= 0)
  109.                        Font_Size = 8;
  110.                if (Wait_Max < 0)
  111.                        Wait_Max = 0;
  112.  
  113.                if ((ac == 3) || (ac == 4))
  114.                        do_it(ac, av);
  115.                else
  116.                {
  117.                        usage(my_name);
  118.                        clean_up(1);
  119.                }
  120.        }
  121.  
  122.        /* Returns resouces to the Amiga */
  123.        clean_up(0);
  124. }
  125.  
  126. do_it(ac, av)
  127. int ac;
  128. char *av[];
  129. {
  130.        char font_name[20], screen_name[81], window_name[81];
  131.        struct TextAttr ta;
  132.        struct TextFont *old_font;
  133.        struct RastPort *rast_port;
  134.  
  135.        /* set up the parameters */
  136.        sprintf(font_name,"%s.font", av[1]);
  137.        strcpy(screen_name, av[2]);
  138.        if (ac == 4)
  139.                strcpy(window_name, av[3]);
  140.        else
  141.                window_name[0] = '\0';
  142.  
  143.        /* my text attributes */
  144.        ta.ta_Name = (UBYTE *) font_name;
  145.        ta.ta_YSize = (long) Font_Size;
  146.        ta.ta_Style = 0L;
  147.        ta.ta_Flags = FPF_DISKFONT;
  148.  
  149.        Font = (struct TextFont *) OpenDiskFont(&ta);
  150.        if (Font == (struct TextFont *) NULL)
  151.        {
  152.                fprintf(stderr,"Cannot find font \"%s %d\"\n", font_name, Font_S
  153. ze);
  154.                clean_up(2);
  155.        }
  156.  
  157.        while (Wait_Max-- >= 0)
  158.        {
  159.                if (get_window(screen_name, window_name))
  160.                /* Window is a global variable and it is set in get_window() */
  161.                {
  162.                        rast_port = Window->RPort;
  163.                        old_font = rast_port->Font;
  164.                        /* see heading comment box 04/20/89 */
  165.                        if (Wait_Max >= 0) Delay(TEN_SECS);
  166.                        if ( SetFont(rast_port, Font))
  167.                                Font = old_font;        
  168.                        break;
  169.                }
  170.                else  if (Wait_Max >= 0) Delay(TEN_SECS);
  171.        }
  172.  
  173.        if (Window == (struct Window *) NULL)
  174.        {
  175.                fprintf(stderr,"Cannot find window \"%s\" in screen \"%s\"\n", w
  176. ndow_name, screen_name);
  177.                clean_up(2);
  178.        }
  179. }
  180.  
  181. get_window(sn, wn)
  182. char *sn;
  183. char *wn;
  184. {
  185.  
  186.        struct Screen *screen;
  187.  
  188.        LockIBase(NULL);
  189.  
  190.        for (screen = IntuitionBase->FirstScreen; screen != (struct Screen *) NU
  191. L; screen = screen->NextScreen)
  192.        {
  193.                if (strncmp(screen->Title, sn, strlen(sn)) == 0)
  194.                {
  195.                        for (Window = screen->FirstWindow; Window != (struct Win
  196. ow *) NULL; Window = Window->NextWindow)
  197.                        {
  198.                                if (wn[0] == '\0')
  199.                                {
  200.                                        if (Window->Title == (UBYTE *) NULL)
  201.                                        {
  202.                                                UnlockIBase(NULL);
  203.                                                return(TRUE);
  204.                                        }
  205.                                }
  206.                                else if (strncmp(Window->Title, wn, strlen(wn)) 
  207. = 0)
  208.                                {
  209.                                        UnlockIBase(NULL);
  210.                                        return(TRUE);
  211.                                }
  212.                        }
  213.                }
  214.        }
  215.        Window = (struct Window *) NULL;
  216.        UnlockIBase(NULL);
  217.        return(FALSE);
  218. }
  219.  
  220. clean_up(code)
  221. int code;
  222. {
  223.        if (Font) CloseFont(Font);
  224.        if (DiskfontBase) CloseLibrary(DiskfontBase);
  225.        if (IntuitionBase) CloseLibrary(IntuitionBase);
  226.        if (GfxBase) CloseLibrary(GfxBase);
  227.        exit(code);
  228. }
  229.  
  230. Open_Libs()
  231. {
  232.        GfxBase = (long ) OpenLibrary("graphics.library", INTUI_REV);
  233.        if (GfxBase == NULL)
  234.        {
  235.                fprintf(stderr,"Cannot open graphics.library\n");
  236.                clean_up(1);
  237.        }
  238.  
  239.        IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library"
  240.  INTUI_REV);
  241.        if (IntuitionBase == (struct IntuitionBase *) NULL)
  242.        {
  243.                fprintf(stderr,"Cannot open intuition.library\n");
  244.                clean_up(1);
  245.        }
  246.  
  247.        DiskfontBase = (long ) OpenLibrary("diskfont.library", INTUI_REV);
  248.        if (DiskfontBase ==  NULL)
  249.        {
  250.                fprintf(stderr,"Cannot open diskfont.library\n");
  251.                clean_up(1);
  252.        }
  253. }
  254.  
  255. print_windows()
  256. {
  257.        struct Window *window;
  258.        struct Screen *screen;
  259.  
  260.        /* Should I lock the IntuitionBase? ... Nah ... */
  261.        for (screen = IntuitionBase->FirstScreen; screen != (struct Screen *) NU
  262. L; screen = screen->NextScreen)
  263.        {
  264.                printf("Screen: \"%s\"\n", screen->Title);
  265.                for (window = screen->FirstWindow; window != (struct Window *) N
  266. LL; window = window->NextWindow)
  267.                        printf("Window: \"%s\"\n", window->Title);
  268.        }
  269. }
  270.  
  271.